<?php

class CPm extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	public function __construct(){
		
		$this->_name=R_PRE."pms";
		
		$this->_primary="pmid";
		
		parent::__construct();
		
	}

	public function createPm($data){
		
		$pmid=$this->insert($data);
		
		return $pmid;
		
	}
	
	public function getPmCounts($uid,$flid,$fromto){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		$select->where("{$this->_name}.{$fromto}_uid = ? ",$uid);
		
		if($flid!=0&&$flid!=null){
		
			$select->where($this->_name.'.flid = ? ',$flid);
			
		}
		
		$select->where("{$this->_name}.{$fromto}_has_del = ? ",'0');
		
		return $db->fetchOne($select);
		
	}
	
	public function getPms($uid,$flid,$fromto,$from,$counts){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$pmflTable=R_PRE.'pmfls';
		
		$memberTable=R_PRE.'members';
		
		$select->join($pmflTable,$pmflTable.'.flid='.$this->_name.'.flid','*');
		
		$fromToArray=array('from'=>'to','to'=>'from');
		
		$select->join($memberTable,$memberTable.'.uid='.$this->_name.".{$fromToArray[$fromto]}_uid",'username');
		
		$select->where("{$this->_name}.{$fromto}_uid = ? ",$uid);
		
		$select->where("{$this->_name}.{$fromto}_has_del = ? ",'0');
		
		if($flid!=0&&$flid!=null){
		
			$select->where($this->_name.'.flid = ? ',$flid);
			
		}
		
		$select->order("{$this->_name}.add_time DESC");
		
		$select->limit($counts, $from);
		
		return $db->fetchAll($select);
		
	}
	
	public function getDetail($pmid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$memberTable=R_PRE.'members';
		
		$select->from($this->_name,'*');
		
		$pmflTable=R_PRE.'pmfls';
		
		$select->join($pmflTable,$pmflTable.'.flid='.$this->_name.'.flid','*');
		
		$select->join("$memberTable AS A",'A.uid='.$this->_name.".from_uid",'username AS from_username');
		
		$select->join("$memberTable AS B",'B.uid='.$this->_name.".to_uid",'username AS to_username');
		
		$select->where($this->_name.'.pmid = ? ',$pmid);
		
		return $db->fetchAll($select);
		
	}
	
	public function setDel($pmidArray,$fromto){
		
		$pmidString=join(',',$pmidArray);
		
		$this->update(array($fromto.'_has_del'=>'1'),"pmid IN ($pmidString)");
		
		$db=$this->getAdapter();
		
		$result=$db->fetchAll("SELECT from_has_del,to_has_del,pmid FROM $this->_name WHERE pmid IN ($pmidString)");
		
		$shouldDelArray=array();
		
		for($i=0;$i<count($result);$i++){
			
			if($result[$i]['from_has_del']==$result[$i]['to_has_del']&&$result[$i]['to_has_del']==1){
				
				$shouldDelArray[]=$result[$i]['pmid'];
				
			}
			
		}
		
		$delResult=0;
		
		if(count($shouldDelArray)>0){
		
			$delResult=$this->delPms($shouldDelArray);
			
		}
		
		return $delResult;
		
	}

	public function delPms($pmidArray){
		
		$db=$this->getAdapter();
		
		$pmidString=join(',',$pmidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE pmid IN ($pmidString)");
		
		return $result;
		
	}
	
	public function editPm($pmid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('pmid = ?', $pmid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getNewPmCounts($uid){
		
		$db=$this->getAdapter();
		
		$select=$db->select();
		
		$select->from($this->_name,'COUNT(*)');
		
		$select->where(" to_uid= ? ",$uid);
		
		$select->where(" has_read='0'");
		
		return $db->fetchOne($select);
		
	}
	

	
}